//package com.jwds.digital.bios.business.rpc.feign.config;
//
//import com.jwds.tdc.framework.application.enums.HeaderEnum;
//import com.jwds.tdc.framework.application.web.interceptor.HystrixRequestInterceptor;
//import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
//import feign.RequestInterceptor;
//import org.springframework.context.annotation.Bean;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Collection;
//import java.util.Enumeration;
//import java.util.Map;
//import java.util.Set;
//import java.util.stream.Collectors;
//
///**
// * @author hesong
// */
//public class BiosFlowFeignConfiguration {
//
//    private static final String TOKEN = "x-rbac-token";
//    private static final String AUTHORIZATION = "Authorization";
//
//    @Bean
//    public RequestInterceptor requestInterceptor() {
//        return template -> {
//
//            if (!HystrixRequestContext.isCurrentThreadInitialized()) {
//                HystrixRequestContext.initializeContext();
//            }
//            HttpServletRequest request = HystrixRequestInterceptor.REQUEST.get();
//
//            if (null == request) {
//                return;
//            }
//            // 获取RequestTemplate中已包含的请求头
//            Map<String, Collection<String>> curHeaders = template.headers();
//            Set<String> curHeaderNames = curHeaders.keySet()
//                    .stream().map(String::toLowerCase).collect(Collectors.toSet());
//
//            Enumeration<String> headerNames = request.getHeaderNames();
//            if (headerNames != null) {
//                while (headerNames.hasMoreElements()) {
//                    String name = headerNames.nextElement();
//                    // 如果RequestTemplate头信息中已经包含了该请求头, 跳过
//                    if (curHeaderNames.contains(name)) {
//                        continue;
//                    }
//                    // 只将header中的CLIENT、USER、x-rbac-token相关的信息转发
//                    if (name.equalsIgnoreCase(HeaderEnum.X_CLIENT_CODE.getName()) ||
//                            name.equalsIgnoreCase(HeaderEnum.X_USER_TOKEN.getName()) ||
//                            name.equalsIgnoreCase(HeaderEnum.X_USER_NAME.getName()) ||
//                            name.equalsIgnoreCase(HeaderEnum.X_NICK_NAME.getName()) ||
//                            name.equalsIgnoreCase(HeaderEnum.X_USER_ID.getName()) ||
//                            name.equalsIgnoreCase(TOKEN)) {
//                        String values = request.getHeader(name);
//                        template.header(name, values);
//                    } else if (name.equalsIgnoreCase(AUTHORIZATION)) {
//                        //处理请求头Authorization: V1#biz-portal#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidXNlcm5hbWUiOiJhZG1pbiIsIm1hbmFnZXIiOiJhZG1pbiIsInZlcnNpb24iOjIsImFwcGlkIjoiYml6LXBvcnRhbCIsImlhdCI6MTY1NzE2NDI2OCwiZXhwIjoxNjU5NzU2MjY4fQ.ZwWzQEKTavjVn8WXvPSZ7iELG4LZ0823-KMkncIA-9w
//                        String values = request.getHeader(name);
//                        String[] arr = values.split("#");
//                        if (arr.length == 3) {
//                            template.header(TOKEN, arr[2]);
//                        }
//                    }
//                }
//            }
//        };
//    }
//
//}
